package com.guchenbo.example.ds.hash;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by CreGu on 2016/5/27.
 */
public class Test {

    private HashMap<String, Integer> map = new HashMap<String, Integer>();
    private List<String> list = new ArrayList<String>();

    public void read(String filePath) {
        try {
            long t1 = System.currentTimeMillis();

            List<String> strings = FileUtils.readLines(new File(filePath));
            for (String str : strings) {

                String ip = findIp(str);
                if (ip != null) {
                    putIp(ip);
                }
            }

            long t2 = System.currentTimeMillis();
            System.out.println((t2 - t1));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void putIp(String ip) {
//
//        map.put(ip, 1);

//        list.add(ip);
        System.out.println(ip);
    }

    public String findIp(String string) {
        Pattern pattern = Pattern.compile("(25[0-5]|2[0-4]\\d|1?\\d\\d?\\.){3}(25[0-5]|2[0-4]\\d|1?\\d\\d?)");
//        Pattern pattern = Pattern.compile("(1\\d\\d|\\d\\d)");
        Matcher matcher = pattern.matcher(string);

        if (matcher.find()) {
            return matcher.group();
        } else {
            return null;
        }

    }

    public int hashFunc2(int key) {
        // non-zero, less than array size, different from hF1
        // array size must be relatively prime to 5, 4, 3, and 2
        return 5 - key % 5;
    }

    public static void main(String[] args) {
        Test test = new Test();
//        System.out.println(test.findIp("http:81"));
        System.out.println(test.findIp("http://10.81.191.32:8088"));

//        test.read(System.getProperty("user.dir") + "/test.log");

        System.out.println(test.hashFunc2(118));
    }
}
